﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cs.Multilingual
{

    public partial class LangControl
    {
        public LangControl()
        {
            InitializeComponent();
        }

        public void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(LangName.Text))
            {
                MessageBox.Show("Language is empty");return;
            }
            if (string.IsNullOrEmpty(Id.Text))
            {
                MessageBox.Show("ID is empty");return;
            }
            var lang = new Lang(LangName.Text);

            if (!lang.Exists())
            {
                MessageBox.Show("Language is not Exists. First Create it.");
                Create.Visibility = Visibility.Visible;
                Rtl.IsEnabled = false;
                return;
            }

            lang[Id.Text] = ContentValue.Text;
        }

        private void LangName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(LangName.Text))
            {
                return;
            }
            var lang = new Lang(LangName.Text);
            try
            { 
                Rtl.IsChecked = lang.RightToLeft;
                if (Rtl.IsChecked != null && Rtl.IsChecked.Value)
                    ContentValue.FlowDirection = FlowDirection.RightToLeft;
                else if (Rtl.IsChecked != null && !Rtl.IsChecked.Value)
                    ContentValue.FlowDirection = FlowDirection.LeftToRight;
                Create.Visibility = Visibility.Collapsed;
                Rtl.IsEnabled = true;
            }
            catch (Exception)
            {
                Create.Visibility = Visibility.Visible;
                Rtl.IsEnabled = false;
            }
        }

        private void Create_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(LangName.Text))
            {
                MessageBox.Show("Language is empty"); return;
            }
            var lang = new Lang(LangName.Text);
            lang.CreateLanguage();

            Rtl.IsChecked = lang.RightToLeft;
            Create.Visibility = Visibility.Collapsed;
            Rtl.IsEnabled = true;
        }

        private void Rtl_OnClick(object sender, RoutedEventArgs e)
        {
            var lang = new Lang(LangName.Text);

            if (!lang.Exists())
            {
                MessageBox.Show("Language is not Exists. First Create it.");
                Create.Visibility = Visibility.Visible;
                Rtl.IsEnabled = false;
                return;
            }

            if (Rtl.IsChecked != null) lang.RightToLeft = Rtl.IsChecked.Value;

            if (Rtl.IsChecked != null && Rtl.IsChecked.Value)
                ContentValue.FlowDirection = FlowDirection.RightToLeft;
            else if (Rtl.IsChecked != null && !Rtl.IsChecked.Value)
                ContentValue.FlowDirection = FlowDirection.LeftToRight;

        }

        private void Id_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(LangName.Text))
            {
                return;
            }
            if (string.IsNullOrEmpty(Id.Text))
            {
                return;
            }
            var lang = new Lang(LangName.Text);

            if (!lang.Exists())
            {
                MessageBox.Show("Language is not Exists. First Create it.");
                Create.Visibility = Visibility.Visible;
                Rtl.IsEnabled = false;
                return;
            }
            //if (lang[Id.Text] != string.Empty)
                ContentValue.Text = lang[Id.Text];
        }
    }
}
